import Vue from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'
import VueAxios from 'vue-axios'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

Vue.use(VueAxios, axios)
Vue.use(ElementUI)

Vue.config.productionTip = false
//是指拦截器
axios.interceptors.request.use(config => {
  console.log('config:',config.url)
  return config
})
//设置接口调用时配套的token值
if(localStorage['token']){
  axios.defaults.headers.common['Authorization'] = localStorage['token']
}
//设置接口地址的公共部分
axios.defaults.baseURL='http://localhost:3000/'

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

router.beforeEach((to, from ,next) => {
  if(to.meta.loginRequest) {
    if(localStorage.getItem('token')!==null){
      next()
    }else{
      next({
        path:'/login',
        query:{
          redirect:to.fullPath
        }
      })
    }
  } else {
    next()  //直接进入页面
  }
})